﻿using Caliburn.Micro;
using KouDai.WinPad.Helper;
using KouDai.WinPad.Models;
using KouDai.WinPad.Parser;
using KouDai.WinPad.Services;
using Newtonsoft.Json;
using NextEngine.HttpEngine;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
using NextEngine.Extens;

namespace KouDai.WinPad.ViewModels
{
    public class ShellPageViewModel : KouDaiScreen
    {
        #region Fields
        private INavigationService _navigationService;
        private List<HotEntity> _hotItems = new List<HotEntity>();
        private ObservableCollection<GroupCollection<HoneyEntity>> _groupItems = new ObservableCollection<GroupCollection<HoneyEntity>>();
        private BindableCollection<ZoomOutModel> _zoomOutItems = new BindableCollection<ZoomOutModel>();

        #endregion

        #region Constrcutor

        public ShellPageViewModel()
        {
            if (DesignMode.DesignModeEnabled)
            {
                LoadDesignData();
            }
        }

        public ShellPageViewModel(INavigationService navigationSerivce)
            : base(navigationSerivce)
        {
            _navigationService = navigationSerivce;
        }

        #endregion

        #region Properties

        public ObservableCollection<GroupCollection<HoneyEntity>> GroupItems
        {
            get { return _groupItems; }
            set
            {
                if (value != _groupItems)
                {
                    _groupItems = value;
                    this.NotifyOfPropertyChange(() => this.GroupItems);
                }
            }
        }

        public BindableCollection<ZoomOutModel> ZoomOutItems
        {
            get { return _zoomOutItems; }
            set { _zoomOutItems = value; }
        }

        public override bool Loading
        {
            get
            {
                return base.Loading;
            }
            set
            {
                if (value != base._loading)
                {
                    base._loading = value;
                    BusyInicator.Instance.IsBusy = value;
                    this.NotifyOfPropertyChange(() => this.Loading);
                }
            }
        }
        #endregion

        #region override

        public override async void LoadDesignData()
        {

            //StorageFile file = await Package.Current.InstalledLocation.GetFileAsync(@"Initial\HotItems.json");
            //string result = await FileIO.ReadTextAsync(file);

            //BackData backData = new BackData(_hotItems);
            //CategoryParser categoryParser = new CategoryParser();
            //categoryParser.ParseData(result, backData);


            //if (this._hotItems == null)
            //{
            //    throw new ArgumentNullException("HotItems");
            //}

            #region LoadZoomInItems
           

            ZoomOutModel model = new ZoomOutModel() { ImageUrl = "http://img.geilicdn.com/bd1897d0e7dc4fa9e6977f36316db1b1.png", GroupName = "少女装" };
            model.SubImageUrlOne = "http://img.geilicdn.com/taobao15866793897_146x219.jpg";
            model.SubImageUrlTwo = "http://img.geilicdn.com/taobao19765708787_146x194.jpg";
            model.Price1 = "234.0";
            model.Price2 = "234.0";
            ZoomOutItems.Add(model);
            GroupCollection<HoneyEntity> collection = new GroupCollection<HoneyEntity>(model);
            List<HoneyEntity> honeyList = new List<HoneyEntity>();
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15866793897_146x219.jpg", RowSpan = 2, ColSpan = 2 ,Price="100000000"});
            honeyList.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrlForIphone = "http://img.geilicdn.com/taobao19765708787_146x194.jpg", RowSpan = 2, ColSpan = 2, Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrlForIphone = "http://img.geilicdn.com/taobao15848113020_146x146.jpg", RowSpan = 2, ColSpan = 2, Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15848325228_146x219.jpg", RowSpan = 2, ColSpan = 2, Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.03, ImageUrlForIphone = "http://img.geilicdn.com/taobao19779004225_146x150.jpg", RowSpan = 2, ColSpan = 2, Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.57, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865634518_146x229.jpg", RowSpan = 2, ColSpan = 2, Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865434962_146x144.jpg", RowSpan = 2, ColSpan = 2, Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15830113145_146x219.jpg", RowSpan = 2, ColSpan = 2, Price = "100" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.15, ImageUrlForIphone = "http://img.geilicdn.com/taobao17077543430_146x167.jpg", RowSpan = 2, ColSpan = 2, Price = "100" });
            foreach (HoneyEntity item in honeyList)
            {
                collection.Add(item);
            }

            model = new ZoomOutModel() { ImageUrl = "http://img.geilicdn.com/2308eedf49fce5a997469f4ddea49644.png", GroupName = "轻熟女" };
            model.SubImageUrlOne = "http://img.geilicdn.com/taobao15866793897_146x219.jpg";
            model.SubImageUrlTwo = "http://img.geilicdn.com/taobao19765708787_146x194.jpg";
            model.Price1 = "234.0";
            model.Price2 = "234.0";
            ZoomOutItems.Add(model);
            GroupCollection<HoneyEntity> collection1 = new GroupCollection<HoneyEntity>(model);
            List<HoneyEntity> honeyList1 = new List<HoneyEntity>();
            honeyList1.Add(new HoneyEntity { ImgRatio = 1.46, ImageUrlForIphone = "http://img.geilicdn.com/taobao19726272617_146x213.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList1.Add(new HoneyEntity { ImgRatio = 1.12, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865557654_146x163.jpg", RowSpan = 4, ColSpan = 4 });
            honeyList1.Add(new HoneyEntity { ImgRatio = 1.38, ImageUrlForIphone = "http://img.geilicdn.com/taobao19776424457_146x201.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList1.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrlForIphone = "http://img.geilicdn.com/taobao19712332041_146x144.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList1.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrlForIphone = "http://img.geilicdn.com/taobao19722332410_146x146.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList1.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15851750545_146x219.jpg", RowSpan = 2, ColSpan = 2 });
            foreach (HoneyEntity item in honeyList1)
            {
                collection1.Add(item);
            }

            model = new ZoomOutModel() { ImageUrl = "http://img.geilicdn.com/060da3aac9660728c8b1d0efb19ddc82.png", GroupName = "男人装" };
            model.SubImageUrlOne = "http://img.geilicdn.com/taobao15866793897_146x219.jpg";
            model.SubImageUrlTwo = "http://img.geilicdn.com/taobao19765708787_146x194.jpg";
            model.Price1 = "234.0";
            model.Price2 = "234.0";
            ZoomOutItems.Add(model);
            GroupCollection<HoneyEntity> collection2 = new GroupCollection<HoneyEntity>(model);
            List<HoneyEntity> honeyList2 = new List<HoneyEntity>();
            honeyList2.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15866793897_146x219.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList2.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrlForIphone = "http://img.geilicdn.com/taobao19765708787_146x194.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList2.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrlForIphone = "http://img.geilicdn.com/taobao15848113020_146x146.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList2.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15848325228_146x219.jpg", RowSpan = 4, ColSpan = 4 });
            honeyList2.Add(new HoneyEntity { ImgRatio = 1.03, ImageUrlForIphone = "http://img.geilicdn.com/taobao19779004225_146x150.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList2.Add(new HoneyEntity { ImgRatio = 1.57, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865634518_146x229.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList2.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865434962_146x144.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList2.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15830113145_146x219.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList2.Add(new HoneyEntity { ImgRatio = 1.15, ImageUrlForIphone = "http://img.geilicdn.com/taobao17077543430_146x167.jpg", RowSpan = 2, ColSpan = 2 });
            foreach (HoneyEntity item in honeyList2)
            {
                collection2.Add(item);
            }

            model = new ZoomOutModel() { ImageUrl = "http://img.geilicdn.com/f03a85baeb420d6bc562483688e5a526.png", GroupName = "童装" };
            model.SubImageUrlOne = "http://img.geilicdn.com/taobao15866793897_146x219.jpg";
            model.SubImageUrlTwo = "http://img.geilicdn.com/taobao19765708787_146x194.jpg";
            model.Price1 = "234.0";
            model.Price2 = "234.0";
            ZoomOutItems.Add(model);
            GroupCollection<HoneyEntity> collection3 = new GroupCollection<HoneyEntity>(model);
            List<HoneyEntity> honeyList3 = new List<HoneyEntity>();
            honeyList3.Add(new HoneyEntity { ImgRatio = 1.46, ImageUrlForIphone = "http://img.geilicdn.com/taobao19726272617_146x213.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList3.Add(new HoneyEntity { ImgRatio = 1.12, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865557654_146x163.jpg", RowSpan = 4, ColSpan = 4 });
            honeyList3.Add(new HoneyEntity { ImgRatio = 1.38, ImageUrlForIphone = "http://img.geilicdn.com/taobao19776424457_146x201.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList3.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrlForIphone = "http://img.geilicdn.com/taobao19712332041_146x144.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList3.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrlForIphone = "http://img.geilicdn.com/taobao19722332410_146x146.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList3.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15851750545_146x219.jpg", RowSpan = 2, ColSpan = 2 });
            foreach (HoneyEntity item in honeyList3)
            {
                collection3.Add(item);
            }

            model = new ZoomOutModel() { ImageUrl = "http://img.geilicdn.com/fd49fb7ae48f17a39a35bc48b609030d.png", GroupName = "科技" };
            model.SubImageUrlOne = "http://img.geilicdn.com/taobao15866793897_146x219.jpg";
            model.SubImageUrlTwo = "http://img.geilicdn.com/taobao19765708787_146x194.jpg";
            model.Price1 = "234.0";
            model.Price2 = "234.0";
            ZoomOutItems.Add(model);
            GroupCollection<HoneyEntity> collection4 = new GroupCollection<HoneyEntity>(model);
            List<HoneyEntity> honeyList4 = new List<HoneyEntity>();
            honeyList4.Add(new HoneyEntity { ImgRatio = 1.46, ImageUrlForIphone = "http://img.geilicdn.com/taobao19726272617_146x213.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList4.Add(new HoneyEntity { ImgRatio = 1.12, ImageUrlForIphone = "http://img.geilicdn.com/taobao15865557654_146x163.jpg", RowSpan = 4, ColSpan = 4 });
            honeyList4.Add(new HoneyEntity { ImgRatio = 1.38, ImageUrlForIphone = "http://img.geilicdn.com/taobao19776424457_146x201.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList4.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrlForIphone = "http://img.geilicdn.com/taobao19712332041_146x144.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList4.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrlForIphone = "http://img.geilicdn.com/taobao19722332410_146x146.jpg", RowSpan = 2, ColSpan = 2 });
            honeyList4.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img.geilicdn.com/taobao15851750545_146x219.jpg", RowSpan = 2, ColSpan = 2 });
            foreach (HoneyEntity item in honeyList4)
            {
                collection4.Add(item);
            }

            GroupItems.Add(collection);
            GroupItems.Add(collection1);
            GroupItems.Add(collection2);
            GroupItems.Add(collection3);
            GroupItems.Add(collection4);

            
            #endregion
            #region AddCategory

            model = new ZoomOutModel() { ImageUrl = "Assets/Images/home_tech.png", GroupName = "分类" };
            GroupCollection<HoneyEntity> collection5 = new GroupCollection<HoneyEntity>(model);
            List<HoneyEntity> honeyList5 = new List<HoneyEntity>();
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_nvzhuang.png", ColSpan = 2, RowSpan = 1 });
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_nvbao.png", ColSpan = 2, RowSpan = 1 });
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_nanxie.png", ColSpan = 2, RowSpan = 1 });
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_peishi.png", ColSpan = 2, RowSpan = 1 });
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_tongzhuang.png", ColSpan = 2, RowSpan = 1 });
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_nvxie.png", ColSpan = 2, RowSpan = 1 });
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_nanzhuang.png", ColSpan = 2, RowSpan = 1 });
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_nanbao.png", ColSpan = 2, RowSpan = 1 });
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_jiaju.png", ColSpan = 2, RowSpan = 1 });
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_shuma.png", ColSpan = 2, RowSpan = 1 });

            foreach (HoneyEntity item in honeyList5)
            {
                collection5.Add(item);
            }
            this.GroupItems.Add(collection5);

            #endregion
           
        }

        private GroupCollection<HoneyEntity> AddCategory()
        {
            ZoomOutModel model = new ZoomOutModel() { ImageUrl = "Assets/Images/home_tech.png", GroupName = "分类" };
            GroupCollection<HoneyEntity> collection5 = new GroupCollection<HoneyEntity>(model);
            List<HoneyEntity> honeyList5 = new List<HoneyEntity>();
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_nvzhuang.png",ColSpan=2,RowSpan=1});
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_nvbao.png", ColSpan = 2, RowSpan = 1});
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_nanxie.png", ColSpan = 2, RowSpan = 1 });
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_peishi.png", ColSpan = 2, RowSpan = 1 });
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_tongzhuang.png", ColSpan = 2, RowSpan = 1 });
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_nvxie.png", ColSpan = 2, RowSpan = 1});
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_nanzhuang.png", ColSpan = 2, RowSpan = 1});
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_nanbao.png", ColSpan = 2, RowSpan = 1});
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_jiaju.png", ColSpan = 2, RowSpan = 1});
            honeyList5.Add(new HoneyEntity { ImgRatio = 1.0, ImageUrlForIphone = "/Assets/Images/category_shuma.png", ColSpan = 2, RowSpan = 1});

            foreach (HoneyEntity item in honeyList5)
            {
                collection5.Add(item);
            }
            return collection5;
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();
            LoadRealGroupData();
        }

        #endregion

        #region Method

        private async void LoadRealGroupData()
        {
            if (!Loading)
            {
                Loading = true;

                StorageFile file = await Package.Current.InstalledLocation.GetFileAsync(@"Initial\HotItems.json");
                string result = await FileIO.ReadTextAsync(file);

                BackData backData = new BackData(_hotItems);
                CategoryParser categoryParser = new CategoryParser();
                categoryParser.ParseData(result, backData);


                if (this._hotItems == null)
                {
                    throw new ArgumentNullException("HotItems");
                }
                List<GroupCollection<HoneyEntity>> groupItems = new List<GroupCollection<HoneyEntity>>();
                foreach (HotEntity subHotItem in _hotItems)
                {
                    GroupCollection<HoneyEntity> collection = await LoadSingleData(subHotItem.OriginalAppName, subHotItem.ImageUrl,subHotItem.CombineAppId);
                    groupItems.Add(collection);
                }
                GroupCollection<HoneyEntity> categroyGroup = AddCategory();
                groupItems.Add(categroyGroup);

                groupItems.ToObservableCollection(this.GroupItems);
                
                Loading = false;
                
            }
           
        }

        private async Task<GroupCollection<HoneyEntity>> LoadSingleData(string name, string imageUrl,string token)
        {
            ZoomOutModel model = null;
            model = new ZoomOutModel() { ImageUrl = imageUrl, GroupName = name };
            GroupCollection<HoneyEntity> collection = new GroupCollection<HoneyEntity>(model);
            BackData backData = await ServiceManager.GetImageWall(token, 0, 20);
            if (backData.IsSuccess)
            {
                List<HoneyEntity> honeyList = (List<HoneyEntity>)backData.ResposeData;
                model.SubImageUrlOne = honeyList[0].ImageUrlForIphone;
                model.SubImageUrlTwo = honeyList[1].ImageUrlForIphone;
                model.Price1 = honeyList[0].Price;
                model.Price2 = honeyList[1].Price;
                ZoomOutItems.Add(model);
                for (int i = 0; i < 6; i++)
                {
                    HoneyEntity item = honeyList[i];
                    if (i == 3)
                    {
                        item.ColSpan = 4;
                        item.RowSpan = 4;
                    }
                    else
                    {
                        item.RowSpan = 2;
                        item.ColSpan = 2;
                    }
                    collection.Add(item);

                }
            }
            return collection;
        }

        #endregion

        #region UIMethod

        public void ItemSelect(ItemClickEventArgs args)
        {
            HoneyEntity selectedItem = (HoneyEntity)args.ClickedItem;
            Debug.WriteLine(selectedItem.ID + selectedItem.ImageUrlForIphone);
            if (selectedItem.ID == null)
            {
                //_navigationService.UriFor<BasicPageViewModel>()
                // .Navigate();

                int index = GroupItems.Where(x => ((ZoomOutModel)x.Key).GroupName == "分类").FirstOrDefault().IndexOf(selectedItem);
                _navigationService.UriFor<SearchPageViewModel>()
                    .WithParam<int>(x=>x.Index,index)
                    .Navigate();
                //Navigate To SearchResultPage
            }
            else
            {
                //Navigate To HoneyDetailPage
            }
            
        }

        public void ItemTitleClick(object obj)
        {
            GroupCollection<HoneyEntity> group = (GroupCollection<HoneyEntity>)obj;
            var groupName = ((ZoomOutModel)group.Key).GroupName;
            GroupTitleClick(groupName);
        }

        public void ListItemClick(ItemClickEventArgs args)
        {
            if (args.ClickedItem is ZoomOutModel)
            {
                ZoomOutModel model = (ZoomOutModel)args.ClickedItem;
                string groupName = model.GroupName;
                GroupTitleClick(groupName);
            }
        }

        private void GroupTitleClick(string groupName)
        {
            HotEntity hotItem = _hotItems.Where(x => x.OriginalAppName.Equals(groupName)).FirstOrDefault();
            if (groupName.Equals("分类"))
            {
                return;
            }
            if (hotItem.SubCateories.Count > 0)
            {
                string json = JsonConvert.SerializeObject(hotItem);
                _navigationService.UriFor<SecondHotPageViewModel>()
                    .WithParam<string>(x => x.HotItemJson, json)
                    .WithParam<string>(x=>x.FatherItemJson,JsonConvert.SerializeObject(_hotItems))
                    .Navigate();
            }
            else
            {
                _navigationService.UriFor<MainPageViewModel>()
                  .WithParam<string>(x => x.CurrentHotItemJson, JsonConvert.SerializeObject(hotItem))
                  .WithParam(x => x.DisplayName, groupName.Trim())
                  .WithParam<string>(x => x.ID, hotItem.CombineAppId)
                  .Navigate();
                
            }
        }

        #endregion
        
    }
}
